﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Specialized;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
    public partial class RentProjectionEditControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.DataBinding += new EventHandler(RentRpojectionEditControl_DataBinding);
        }

        void RentRpojectionEditControl_DataBinding(object sender, EventArgs e)
        {
            User u = Session["User"] as User;
            FillBed();
            btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
            btnInsert.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);

            if (DataItem is Telerik.Web.UI.GridInsertionObject)
            {
                string apn = Session["AddRentalUnitApn"].ToString();
                DataAccess da = new DataAccess();
                DataTable dt = da.GetDataTable("lookupRentProjection", new string[] { "@Apn" }, new object[] { apn });
                DataRow dr = dt.Rows[0];

                ListDictionary values = new ListDictionary();
                values["Apn"] = dr["Apn"];
                values["PropertyLocation"] = dr["PropertyLocation"];
                values["ZipCode"] = dr["ZipCode"];

                values["PurchasePrice"] = dr["PurchasePrice"];
                values["ClosingCost"] = dr["ClosingCost"];
                //values["RehabCost"] = dr["RehabCost"];
                values["RentMonth"] = dr["RentMonth"];
                values["HOAMonth"] = dr["HOAMonth"];

                //values["RenterName"] = dr["RenterName"];
                //values["RenterPhone"] = dr["RenterPhone"];
                values["DateRentedFrom"] = dr["DateRentedFrom"];
                values["DateRentedTo"] = dr["DateRentedTo"];

                values["ManagementCompany"] = dr["ManagementCompany"];
                values["Contact"] = dr["Contact"];
                values["CompanyPhone"] = dr["CompanyPhone"];
                values["CompanyAddress"] = dr["CompanyAddress"];

                (DataItem as Telerik.Web.UI.GridInsertionObject).SetupValues(values);

                FillInvestor(dr["InvestorId"] == DBNull.Value ? -1 : Convert.ToInt32(dr["InvestorId"]));
                hdRentProjectionUnitId.Value = "-1";
                //DataItem = dt.DefaultView[0];
            }
            else
            {
                /*tbPurchasePrice.Value = Convert.ToDouble(DataBinder.Eval(DataItem, "PurchasePrice"));
                tbClosingCost.Value = Convert.ToDouble(DataBinder.Eval(DataItem, "ClosingCost"));
                tbRehabCost.Value = Convert.ToDouble(DataBinder.Eval(DataItem, "RehabCost"));
                tbRentMonth.Value = Convert.ToDouble(DataBinder.Eval(DataItem, "RentMonth"));
                tbHOAMonth.Value = Convert.ToDouble(DataBinder.Eval(DataItem, "HOAMonth"));*/
                FillInvestor(DataBinder.Eval(DataItem, "InvestorId") == DBNull.Value ? -1 : Convert.ToInt32(DataBinder.Eval(DataItem, "InvestorId")));
                Utilities.SetValueToCombo(edBedrooms, DataBinder.Eval(DataItem, "Bedrooms"));
                hdRentProjectionUnitId.Value = DataBinder.Eval(DataItem, "RentProjectionUnitId") == DBNull.Value ? "-1" : Convert.ToInt32(DataBinder.Eval(DataItem, "RentProjectionUnitId")).ToString();
            }
            ddlInvestor.Enabled = (u.UserRole != UserRole.InvestorUser);
        }

        protected void tb_TextChanged(object sender, EventArgs e)
        {
            
        }

        protected void FillInvestor(int userId)
        {
            ddlInvestor.Items.Clear();
            DataAccess da = new DataAccess();
            DataTable dt = da.ExecuteSQL("select * from UserAccounts where [Role] = 'IU' order by FName, LName");

            ddlInvestor.Items.Add(new ListItem("(Select)", "-1"));
            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                ddlInvestor.Items.Add(new ListItem(dt.Rows[i]["FName"].ToString() + " " + dt.Rows[i]["LName"].ToString(), dt.Rows[i]["UserId"].ToString()));
                if (userId == Convert.ToInt32(dt.Rows[i]["UserId"]))
                    ddlInvestor.SelectedValue = userId.ToString();
            }

            if (ddlInvestor.SelectedIndex == -1)
                ddlInvestor.SelectedIndex = 0;
        }

        protected void FillBed()
        {
            edBedrooms.Items.Clear();
            for (int i = 1; i <= 20; i++)
            {
                edBedrooms.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }

        }

        public int? RentProjectionUnitId
        {
            get
            {
                int? result = null;
                if (hdRentProjectionUnitId.Value != "-1")
                    result = Convert.ToInt32(hdRentProjectionUnitId.Value);
                return result;
            }
        }
        
        object _dataItem = null;

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
    }
}